﻿using System;
using System.Data.Common;

namespace MicroDal
{
    internal static class DynamicMapper
    {
        private static Func<DbDataReader, string, Type, object> FIELD_INITIALIZER = MapGenerator.READ_FIELD.Compile();

        public static dynamic Map(DbDataReader reader)
        {
            var result = new DynamicDto();
            for (int i = 0; i < reader.FieldCount; i++)
            {
                var name = reader.GetName(i);
                result.AddProperty(name, FIELD_INITIALIZER(reader, name, reader.GetFieldType(i)));
            }
            return result;
        }
    }
}
